<?php

class RoleAction extends CommonAction{
	
	private $role;
	private $menu;
	
	public function __construct(){
		parent::__construct();
		$this->role = D('Role');
		$this->menu = D('Menu');
	}
	
	
	public function index(){
		$roles = $this->role->getAllRoles();
		$this->assign('roles', $roles);
		//取出所有菜单
		$Menu = D('Menu');
		$menu = $Menu->getTopMenu();
		foreach($menu as $key=>$val){ //top
			$menu[$key]['sub'] = $Menu->getSubMenu($val['id']);
		}
		$this->assign('menu',$menu);
		$this->display();
	}
	
	public function add(){
		$this->display();
	}
	
	public function insert(){
		$data['name'] =$_REQUEST['name'];
		$data['sort'] =$_REQUEST['sort'];
		$data['status'] =1;
		$data['menus'] = $_REQUEST['menus'];
		$rid =$this->role->data($data)->add();
		if($rid){
			$this->success("success");
		}else{
			$this->error("error");
		}
	}
	/**
	 * 更新
	 * 
	 */
	public function update(){
		$data['name'] =$_REQUEST['name'];
		$data['sort'] =$_REQUEST['sort'];
		$data['status'] =$_REQUEST['status'];
		$data['menus'] = $_REQUEST['menus'];
		$data['id'] = $_REQUEST['id'];
		if($this->role->save($data)){
			echo 1;
		}else{
			echo 0;
		}
	}
	
	public function delete(){
		$id = $_REQUEST['id'];
		echo $this->role->where('id='.$id)->delete()?1:0;
	}
	/**
	 * 取得角色权限
	 */
	public function getRoleMenus(){
		$roleid = $_REQUEST['id'];
		$role = $this->role->getById($roleid);
		$role['menuArray'] = explode(',', $role['menus']);
		echo json_encode($role);
		
	}
	
	
}

?>